﻿{
  "metadata": {
    "name": "Python 3",
    "kernelspec": {
      "language": "scala",
      "name": "spark2-scala"
    },
    "language_info": {
      "codemirror_mode": "text/x-scala",
      "file_extension": ".scala",
      "mimetype": "text/x-scala",
      "name": "scala",
      "pygments_lexer": "scala"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2,
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "# Get Gapminder Data"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\n%matplotlib inline    \nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\nmpl.rcParams[\u0027font.sans-serif\u0027] \u003d [\u0027KaiTi\u0027, \u0027SimHei\u0027, \u0027FangSong\u0027]  # 汉字字体,优先使用楷体，如果找不到楷体，则使用黑体\nmpl.rcParams[\u0027font.size\u0027] \u003d 12  # 字体大小\nmpl.rcParams[\u0027axes.unicode_minus\u0027] \u003d False  # 正常显示负号\n"
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\npopDataURL \u003d \"https://gitee.com/cloudcoder/data-visual/raw/master/data/gapminder.tsv\"\npopData \u003d pd.read_csv(popDataURL, delimiter\u003d\u0027\\t\u0027, dtype\u003d({\u0027year\u0027:int}))\n"
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\npopData[\u0027pop\u0027]\u003dpopData[\u0027pop\u0027]/1000000\npopData"
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nSpainData \u003d popData[popData[\u0027country\u0027]\u003d\u003d\u0027Spain\u0027]\nUKData \u003d popData[popData[\u0027country\u0027]\u003d\u003d\u0027United Kingdom\u0027]\nIndiaData \u003d popData[popData[\u0027country\u0027]\u003d\u003d\u0027India\u0027]\nUSData \u003d popData[popData[\u0027country\u0027]\u003d\u003d\u0027United States\u0027]"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "# Matplotlib"
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nimport matplotlib.pyplot as plt\nplt.plot(UKData[\u0027year\u0027],UKData[\u0027pop\u0027])\nplt.ylabel(\u0027Population in millions\u0027)\nplt.xlabel(\u0027Year\u0027)\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\n\n\ndata \u003d popData[popData[\u0027year\u0027]\u003d\u003d2007]\n\nplt.scatter(\u0027country\u0027, \u0027gdpPercap\u0027, s\u003d\u0027pop\u0027, data\u003ddata)\nplt.xticks([])\nplt.xlabel(\u0027Countries\u0027)\nplt.ylabel(\u0027GDP Per Cap\u0027)\nplt.show()"
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\n \nUSData.plot.bar(x\u003d\u0027year\u0027,y\u003d\u0027gdpPercap\u0027)\nplt.savefig(\"pandas1.png\")"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "# Seaborn"
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nimport seaborn as sns\nsns.regplot(x\u003d\"year\", y\u003d\"pop\", data\u003dSpainData)"
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nsns.regplot(x\u003d\"year\", y\u003d\"pop\", data\u003dSpainData, order\u003d2, ci\u003dNone)"
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nf \u003d plt.figure(figsize\u003d(12, 4))\n \ngs \u003d f.add_gridspec(1, 3)\n \nax \u003d f.add_subplot(gs[0, 0])\nsns.regplot(x\u003d\"year\", y\u003d\"pop\", data\u003dSpainData,order\u003d1)\n \nax \u003d f.add_subplot(gs[0, 1])\nsns.regplot(x\u003d\"year\", y\u003d\"pop\", data\u003dSpainData,order\u003d2)\n \nax \u003d f.add_subplot(gs[0, 2])\nsns.regplot(x\u003d\"year\", y\u003d\"pop\", data\u003dSpainData,order\u003d3)\n \nf.tight_layout()\n \nplt.savefig(\"seaborn2.png\")"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "# Plotnine (ggplot)"
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\nfrom plotnine import *\n(ggplot(IndiaData,aes(\u0027year\u0027,\u0027pop\u0027))\n + geom_line(color\u003d\u0027Blue\u0027,size\u003d2)\n + theme_light()\n + theme(figure_size\u003d(6,3))\n + ggtitle(\u0027India Population Growth\u0027)\n)"
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\n(ggplot(popData, aes(\u0027year\u0027,\u0027pop\u0027, fill\u003d\u0027continent\u0027))\n + geom_col(show_legend\u003dFalse)\n + facet_wrap(\u0027continent\u0027, scales\u003d\u0027free\u0027)\n + theme_light()\n + theme(panel_spacing\u003d0.5, figure_size\u003d(10,5))\n)"
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\n(ggplot(popData[popData[\u0027year\u0027]\u003d\u003d2007], aes(\u0027country\u0027,\u0027gdpPercap\u0027,size\u003d\u0027pop\u0027,color\u003d\u0027continent\u0027))\n + geom_point()\n #+ theme_void()\n #+ theme(axis_line_x\u003delement_line(), axis_title_y\u003delement_text(rotation\u003d90))\n + theme(axis_text_x\u003delement_text(\u0027\u0027))\n + ggtitle(\u0027Countries\u0027)\n)"
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "autoscroll": "auto"
      },
      "outputs": [],
      "source": "%python\n!pip install plotnine"
    },
    {
      "cell_type": "raw",
      "metadata": {
        "format": "text/plain"
      },
      "source": "%python\n"
    }
  ]
}